package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.Contract;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

/**
 * <p>
 * 合同表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-21
 */
@Mapper
public interface ContractMapper extends BaseMapper<Contract> {

    @Select("<script>" +
            "SELECT N.* FROM (SELECT M.*, " +
            " dad.department_id AS department_id1, " +
            " dad2.department_id AS department_id2, " +
            " dad3.department_id AS department_id3 " +
            "FROM ( SELECT a.*, b.username AS username, d.name AS departmentName " +
            "FROM dragon_contract a " +
            " LEFT JOIN dragon_admin b ON a.admin_id = b.id " +
            " LEFT JOIN dragon_department d ON d.id = a.department_id  " +
            " ${ew.customSqlSegment} ) M " +
            " LEFT JOIN dragon_admin_department dad ON M.payee = dad.admin_id " +
            " LEFT JOIN dragon_admin_department dad2 ON M.production_person = dad2.admin_id " +
            " LEFT JOIN dragon_admin_department dad3 ON M.delivery_officer = dad3.admin_id ORDER BY M.create_time DESC)N" +
            "</script>")
    @DataScope(adminField = "admin_id,delivery_officer,production_person", deptField = "department_id,department_id1,department_id2,department_id3",deptAlias = "N", adminAlias = "N", authCodeString = "contract")
    Page<Contract> contractList(Page<Contract> contractPage, @Param(Constants.WRAPPER) QueryWrapper wrapper);

    @Select("select a.*,b.username as username, b.phone as userPhone from dragon_contract a  " +
            "left join dragon_admin b on a.admin_id = b.id " +
            " where a.id = #{id} ")
    Contract selectOneById(Integer id);

    /**
     * 合同 年金额
     * @return
     */
    @Select("SELECT SUM(dc.money) FROM dragon_contract dc " +
            "WHERE dc.deleted = 0 and dc.status = 1 ")
    @DataScope(adminAlias = "dc",adminField = "admin_id",deptAlias = "dc",deptField = "department_id",  authCodeString = "contract")
    Double selectYearContractMoney();

    /**
     * 合同 月金额
     * @return
     */
    @Select("SELECT SUM(dc.money) FROM dragon_contract dc " +
            "WHERE dc.deleted = 0 and dc.status = 1 and LEFT(dc.order_time,7) = LEFT(NOW(),7)")
    @DataScope(adminAlias = "dc",adminField = "admin_id",deptAlias = "dc",deptField = "department_id",  authCodeString = "contract")
    Double selectMonthContractMoney();

    /**
     * 获取 合同最大编号
     * @param code
     * @return
     */
    @Select("SELECT MAX(num) FROM dragon_contract WHERE num LIKE CONCAT(#{code},'%')")
    String maxRoundInoutNumber(String code);

    /**
     * 统计 合同数量
     * @param wrapper 条件
     * @return
     */
    @Select("SELECT count(*) FROM dragon_contract ${ew.customSqlSegment}")
    @DataScope(adminField = "admin_id", deptField = "department_id", authCodeString = "contract")
    Long selectContractCount(@Param(Constants.WRAPPER) QueryWrapper wrapper);
}
